import { _decorator, CCString, Component, Node, tween, UIOpacity } from 'cc';
import { Logger } from 'db://assets/util';
import { Printer } from '../../components/Printer';
const { ccclass, property, menu } = _decorator;


@ccclass('MajorContainer')
@menu('container/Major')
export class MajorContainer extends Component {
    #logger = new Logger('主对话容器::MajorContainer')

    @property(CCString)
    context: string;

    style: string;

    get UIOpacity() {
        return this.getComponent(UIOpacity)
    }

    protected onEnable(): void {
        this.#logger.debug("#start")
        this.UIOpacity.opacity = 0
        this.fadeIn(() => {
            this.getComponent(Printer).printAnime()
        })
    }

    onLoad() {
        this.#logger.debug("#onLoad")
        this.getComponent(Printer).printerStr = this.context
    }

    start() {

    }


    protected onDisable(): void {

    }

    fadeIn(cb = () => { }) {
        tween(this.UIOpacity)
            .to(0.2, { opacity: 255 }, { easing: 'fade' })
            .call(cb)
            .start()
    }

    fadeOut(cb = () => { }) {
        tween(this.UIOpacity)
            .to(0.2, { opacity: 0 }, { easing: 'fade' })
            .call(cb)
            .start()
    }

    clear(): Promise<void> {
        return new Promise(resolve => {
            this.fadeOut(() => {
                this.node.active = false
                resolve()
            })
        })
    }
}
